package com.gui.proxy.jdk;

import com.gui.proxy.staticProxy.StaticProxyService;
import com.gui.proxy.impl.StaticProxyServiceImpl;

import java.lang.reflect.Proxy;
import java.util.Arrays;

public class JdkProxyDemo {

    public static void main(String[] args) {

        ClassLoader classLoader = JdkProxyDemo.class.getClassLoader();
        Class<?>[] interfaces = {StaticProxyService.class};

        StaticProxyService service = new StaticProxyServiceImpl();

        StaticProxyService proxyService = (StaticProxyService) Proxy.newProxyInstance(classLoader, interfaces, (proxy, method, argArray) -> {
            System.out.println("前置增强..." + Arrays.toString(argArray));
            Object result = method.invoke(service, argArray);
            System.out.println("后置增强..." + result);
            return result;
        });

        String result = proxyService.foo("动态代理");
        System.out.println(result);
    }
}
